package com.tensquare.recruit.service;

import com.tensquare.recruit.dao.RecruitDao;
import com.tensquare.recruit.pojo.Enterprise;
import com.tensquare.recruit.pojo.Recruit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:zimmor
 * @Date: 2021/2/28 19:59
 * @Description 招聘业务逻辑层
 * @version: 1.0.0
 */
@Service
public class RecruitService {
    @Autowired
    private RecruitDao recruitDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 获取推介列表
     * @return
     */
    public List<Recruit> findByStateOrderByCreatetimeDesc(){
        return recruitDao.findByStateOrderByCreatetimeDesc("2");
    }

    /**
     * 获取最新职位列表
     * @return
     */
    public List<Recruit> findByStateNotOrderByCreatetieDesc(){
        return recruitDao.findTop12ByStateNotOrderByCreatetimeDesc("0");
    }

    /**
     * 查询所有
     * @return
     */
    public List<Recruit> findAll() {
        return recruitDao.findAll();
    }

    /**
     * 新增招聘信息
     */
    public void add(Recruit recruit) {
        recruit.setId(idWorker.nextId()+"");
        recruitDao.save(recruit);
    }

    /**
     * 删除招聘信息
     * @param id
     */
    public void delete(String id) {
        recruitDao.deleteById(id);
    }

    /**
     * 修改招聘信息
     * @param recruit
     */
    public void update(Recruit recruit) {
        recruitDao.save(recruit);
    }

    /**
     * 根据id查找
     * @param id
     * @return
     */
    public Recruit findById(String id) {
        return recruitDao.findById(id).get();
    }

    /**
     * 条件查询
     * @return
     */
    public List<Recruit> findSearch(Recruit recruit) {
        return recruitDao.findAll(new Specification<Recruit>() {
            @Override
            public Predicate toPredicate(Root<Recruit> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<>();
                if(recruit.getJobname()!=null && !"".equals(recruit.getJobname())) {
                    Predicate jobname = criteriaBuilder.like(root.get("jobname").as(String.class), "%" + recruit.getJobname() + "%");
                    list.add(jobname);
                }
                if(recruit.getLabel()!=null && !"".equals(recruit.getLabel())) {
                    Predicate label = criteriaBuilder.like(root.get("label").as(String.class), "%" + recruit.getLabel() + "%");
                    list.add(label);
                }
                Predicate[] predicates = new Predicate[list.size()];
                list.toArray(predicates);
                return criteriaBuilder.and(predicates);
            }
        });
    }

    /**
     * 分页查询
     * @param recruit
     * @param page
     * @param size
     * @return
     */
    public Page findSearch(Recruit recruit, int page, int size) {
        Specification specification = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<>();
                if(recruit.getJobname()!=null && !"".equals(recruit.getJobname())) {
                    Predicate jobname = criteriaBuilder.like(root.get("jobname").as(String.class), "%" + recruit.getJobname() + "%");
                    list.add(jobname);
                }
                if(recruit.getLabel()!=null && "".equals(recruit.getLabel())) {
                    Predicate label = criteriaBuilder.like(root.get("label").as(String.class), "%" + recruit.getLabel() + "%");
                    list.add(label);
                }
                Predicate[] predicates = new Predicate[list.size()];
                list.toArray(predicates);
                return criteriaBuilder.and(predicates);

            }
        };
        Pageable pageable = PageRequest.of(page-1, size);
        return recruitDao.findAll(specification, pageable);
    }
}
